﻿/***********************************************************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products.
* No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
* applicable laws, including copyright laws. 
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NON-INFRINGEMENT.  ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY
* LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR
* ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability 
* of this software. By using this software, you agree to the additional terms and conditions found by accessing the 
* following link:
* http://www.renesas.com/disclaimer
*
* Copyright (C) 2018, 2022 Renesas Electronics Corporation. All rights reserved.
***********************************************************************************************************************/

/***********************************************************************************************************************
* File Name        : Config_UART5_user.c
* Component Version: 1.5.0
* Device(s)        : R7F702300EABA
* Description      : This file implements device driver for Config_UART5.
***********************************************************************************************************************/
/***********************************************************************************************************************
Pragma directive
***********************************************************************************************************************/
/* Start user code for pragma. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "r_cg_macrodriver.h"
#include "r_cg_userdefine.h"
#include "Config_UART5.h"
/* Start user code for include. Do not edit comment generated here */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "ringbuffer.h"
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
extern volatile uint8_t  * gp_uart5_tx_address;       /* uart5 transmit data address */
extern volatile uint16_t   g_uart5_tx_count;          /* uart5 transmit data number */
extern volatile uint8_t  * gp_uart5_rx_address;       /* uart5 receive data address */
extern volatile uint16_t   g_uart5_rx_count;          /* uart5 receive data number */
extern volatile uint16_t   g_uart5_rx_length;         /* uart5 receive data length */
void r_Config_UART5_callback_receiveend(void);
void r_Config_UART5_callback_sendend(void);
void r_Config_UART5_callback_error(uint32_t err_type);
/* Start user code for global. Do not edit comment generated here */
volatile uint8_t uart5_rx_one_byte;
struct ringbuffer uart5_rx_rb;
uint8_t *pbuf_uart5_rx_rb = NULL;
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
* Function Name: R_Config_UART5_Create_UserInit
* Description  : This function adds user code after initializing UART module.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/

void R_Config_UART5_Create_UserInit(void)
{
    /* Start user code for user init. Do not edit comment generated here */
    if (pbuf_uart5_rx_rb == NULL)
    {
        pbuf_uart5_rx_rb = malloc(1024);
    }
    ringbuffer_init(&uart5_rx_rb, pbuf_uart5_rx_rb, 1024);
    /* End user code. Do not edit comment generated here */
}

/***********************************************************************************************************************
* Function Name: r_Config_UART5_interrupt_receive
* Description  : This function is UART5 receive interrupt service routine.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
#pragma interrupt r_Config_UART5_interrupt_receive(enable=false, channel=438, fpu=true, callt=false)
void r_Config_UART5_interrupt_receive(void)
{
#if 0
    uint8_t  rx_data_8;
    uint16_t temp;
    rx_data_8 = RLIN35.LURDR.UINT16;

    temp = g_uart5_rx_count;
    if (g_uart5_rx_length > temp)
    {
        *gp_uart5_rx_address = rx_data_8;
        gp_uart5_rx_address++;
        g_uart5_rx_count++;
        temp = g_uart5_rx_count;
        if (g_uart5_rx_length == temp)
        {
            r_Config_UART5_callback_receiveend();
        }
    }
#else
    uint8_t  rx_data_8;
    rx_data_8 = RLIN35.LURDR.UINT16;
    if (0 == ringbuffer_putchar(&uart5_rx_rb, rx_data_8))
    {
        ringbuffer_putchar_force(&uart5_rx_rb, rx_data_8);
    }
#endif
}

/***********************************************************************************************************************
* Function Name: r_Config_UART5_interrupt_error
* Description  : This function is UART5 error interrupt service routine.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
#pragma interrupt r_Config_UART5_interrupt_error(enable=false, channel=439, fpu=true, callt=false)
void r_Config_UART5_interrupt_error(void)
{
    uint32_t err_type;
    *gp_uart5_rx_address = RLIN35.LURDR.UINT16;
    err_type = (uint8_t)(RLIN35.LEST);
    RLIN35.LEST &= (uint8_t) ~_UART_CLEAR_ERROR_FLAG;
    r_Config_UART5_callback_error(err_type);
}

/***********************************************************************************************************************
* Function Name: r_Config_UART5_interrupt_send
* Description  : This function is UART5 send interrupt service routine.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
#pragma interrupt r_Config_UART5_interrupt_send(enable=false, channel=437, fpu=true, callt=false)
void r_Config_UART5_interrupt_send(void)
{
    if (g_uart5_tx_count > 0U)
    {
        RLIN35.LUTDR.UINT16= *gp_uart5_tx_address;
        gp_uart5_tx_address++;
        g_uart5_tx_count--;
    }
    else
    {
        r_Config_UART5_callback_sendend();
    }
}

/***********************************************************************************************************************
* Function Name: r_Config_UART5_callback_sendend
* Description  : This function is UART5 send end callback service routine.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void r_Config_UART5_callback_sendend(void)
{
    /* Start user code for r_Config_UART5_callback_sendend. Do not edit comment generated here */
    /* End user code. Do not edit comment generated here */
}

/***********************************************************************************************************************
* Function Name: r_Config_UART5_callback_error
* Description  : This function is UART5 error callback service routine.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void r_Config_UART5_callback_error(uint32_t err_type)
{
    /* Start user code for r_Config_UART5_callback_error. Do not edit comment generated here */
    /* End user code. Do not edit comment generated here */
}

/***********************************************************************************************************************
* Function Name: r_Config_UART5_callback_receiveend
* Description  : This function is UART5 receive end callback service routine.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void r_Config_UART5_callback_receiveend(void)
{
    /* Start user code for r_Config_UART5_callback_receiveend. Do not edit comment generated here */
    /* End user code. Do not edit comment generated here */
}

/* Start user code for adding. Do not edit comment generated here */
typedef enum UART5_STATE
{
    eUart5Init = 0x00000001,
    eUart5Rx = 0x00000002,
    eUart5Tx = 0x00000004,
    eUart5TestTx = 0x00000008,
    eUart5Idle = 0x0000FFFF
} E_UART5_STATE;
void Uart5Proc(uint32_t u32Cmd, uint32_t u32Tick)
{
    static char * strtmp;
    static uint32_t sState = 0x00000000;
    static uint32_t sWaitTickCount = 0x00000000;

    static E_UART5_STATE eUart5State = eUart5Rx;
    if (sWaitTickCount > 0)
    {
        sWaitTickCount--; //未到时间�??
        return;
    }
    
    switch(eUart5State)
    {
        case eUart5Rx:
            {
                uint32_t u32DataLen = ringbuffer_data_len(&uart5_rx_rb);
                if (u32DataLen > 0)
                {
                    //parse the rx data.
                    unsigned int u32Temp;
                    uint8_t *pbufTemp = (uint8_t *)(void *)malloc(u32DataLen);
                    u32Temp = ringbuffer_get(&uart5_rx_rb, pbufTemp, u32DataLen);
                    if (u32Temp != u32DataLen)
                    {
                        //some error may be  occur!
                    }
                }
            }
            break;
        default:
            break;
    }
}
/* End user code. Do not edit comment generated here */
